<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Classes extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('user_profiles_model', 'classes_model', 'users_model'));
        $this->load->helper(array('form'));
    }

    function index() {
        $data = array();
        $data['classes'] = $this->classes_model->getClasses();
        $this->ingesup->render('classes/index', $data);
    }

    function edit($classe_id){
        if (isset($_POST['submit']) && isset($_POST['student'])){
            foreach ($_POST['student'] as $key => $value)  {
                $this->classes_model->newStudent($classe_id, $value);
            }
        }

        $data['class_info']     = $this->classes_model->getClasses($classe_id);
        $data['classStudents']  = $this->classes_model->getStudentByClass($classe_id);
        $data['freeStudents']   = $this->classes_model->getFreeStudents();
        $this->ingesup->render('/classes/edit', $data);
    }

    function deleteStudent($class_id, $student_id){
        $this->classes_model->removeStudent($student_id);
        redirect('classes/edit/'.$class_id);
    }

    function add(){
        if (isset($_POST['submit']) && !empty($_POST['class'])){
            if ($this->classes_model->addClass($_POST['class'])){
                redirect('/classes/index');
            }
        }
        $this->ingesup->render('/classes/add');
    }

    function deleteClass($class_id){
         $this->classes_model->removeClass($class_id);
         redirect('/classes/index');
    }
}